home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / applySprite.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  8.0 KB  |  193 lines

  1. //
  2. //  ====================== applySprite ======================
  3. //
  4. //  SYNOPSIS
  5. //      Apply sprite hardware rendering to a particle shape.
  6. //
  7. //  NOTE: This version of applySprite is updated and improved for version 3.0.
  8. //
  9. //  If you set "cycleEnabled", the sprites will cycle through the given
  10. //  image once during the lifetime of the particle. 
  11. // 
  12. //  If you set "randomIndex," a random index will be assigned at particle
  13. //  birth time.  (This option and "cycleEnabled" are mutually exclusive,
  14. //  since if you set both, the cycling will immediately override the random
  15. //  index.)  
  16. //
  17. //  applySprite adds controls for cycleEnable and randomIndex so you can
  18. //  toggle back and forth between one and the other.  
  19. //  If you set both  cycleEnable and randomIndex false, all particles will
  20. //  use the same image all the time.
  21. //
  22. //  The expression for spriteNumPP is written in terms of the startCycleExtension
  23. //  and endCycleExtension of the file texture.  If you want to add more images,
  24. //  all you need do is modify those attributes.
  25. // 
  26. //  If you want to change the sprite image after creating the sprites, you'll
  27. //  need to edit the file texture and possibly change the keys on
  28. //  the frameExtension attribute. See the Using Maya documentation.
  29. //
  30. //  To see the sprites, of course, you must set both smooth shading and 
  31. //  hardware texturing on (from the Shading menu).
  32. //
  33. //  EXAMPLES OF USAGE
  34. //
  35. //      particle;
  36. //      applySprite( "particleShape1", "funnyFace", 1,30, 0,1, 3.0,6.0 )
  37. //
  38. //        This adds sprites "funnyFace.1,"...,"funnyFace.30" to particleShape1.
  39. //        It selects randomly one sprite for each particle, and that
  40. //        particle keeps that sprite throughout its lifetime.  Each
  41. //        particle lives a random time between 3 and 6 seconds.
  42. //  
  43. //      particle;
  44. //      applySprite( "particleShape1", "funnyFace", 1,30, 1,0, 3.0,6.0 )
  45. //          
  46. //        Same as above, but each particle will cycle over the complete
  47. //        sequence of sprites during its lifetime, starting with 1.
  48. //        If you were to make the first call to applySprite, then go to
  49. //        the channel box and set randomIndex to false and cycleEnabled to
  50. //        true, you'd get the same results as from the second call.
  51. //
  52. //  INPUT ARGUMENTS
  53. //
  54. //      string $particleShape   The name of the particle shape. Must be the shape.
  55. //        string $imageName        The name of the base image
  56. //        int $startImage         The first image frame to use
  57. //        int $endImage            The end image frame to use
  58. //        int $cycleEnabled        Use image cycling yes/no
  59. //      int $randomIndex       Use random per-particle offset into the sequence
  60. //        float $minLifespan        The minimum lifespan (for cycling)
  61. //        float $maxLifespan        The maximum lifespan (for cycling)
  62.  
  63. global proc int applySprite(
  64.                         string $particleShape,
  65.                         string $imageName,
  66.                         int $startImage, int $endImage, 
  67.                         int $cycleEnabled, int $randomIndex, 
  68.                         float $minLifespan, float $maxLifespan )
  69. {
  70.     // Verify that particle object exists
  71.     //
  72.     if (!particleExists( $particleShape )) 
  73.     {
  74.         print("Error: particle shape " + $particleShape + " not found.\n");
  75.         return 1;
  76.     }
  77.  
  78.     // Set sprite rendering attributes in the particle object
  79.     //
  80.     select -r $particleShape;
  81.     setAttr ($particleShape+".particleRenderType") 5;
  82.  
  83.     // Check whether sprite attributes have been added and if not, add them.
  84.     // Actually we just check one, since we assume if one got added, they all did.
  85.     //
  86.     string $test[] = `listAttr -st spriteTwist $particleShape`;
  87.     if (size($test) == 0)
  88.     {
  89.         addAttr -is true -ln "spriteTwist" 
  90.             -at "float" -min -180 -max 180 -dv 0.0 
  91.             $particleShape;
  92.         addAttr -is true -ln "spriteScaleX" -dv 1.0 $particleShape;
  93.         addAttr -is true -ln "spriteScaleY" -dv 1.0 $particleShape;
  94.         addAttr -is true -ln "spriteNum" -at long -dv 1 $particleShape;
  95.         addAttr -is true -ln "useLighting" -at bool -dv false $particleShape;
  96.     }
  97.     setAttr ($particleShape+".depthSort") 1;
  98.  
  99.     // Make a shader
  100.     //
  101.     string $lambert = `shadingNode -asShader lambert`;
  102.     string $lambertSG   = $lambert + "SG";
  103.     sets -renderable true -noSurfaceShader true -empty -name $lambertSG;
  104.     connectAttr -f ($lambert+".outColor") ($lambertSG+".surfaceShader");
  105.  
  106.     // Make a file texture.
  107.     //
  108.     string $fileTex = `shadingNode -asTexture file`;
  109.     string $placeTex = `shadingNode -asUtility place2dTexture`;
  110.     connectAttr -f ($placeTex+".coverage") ($fileTex+".coverage");
  111.     connectAttr -f ($placeTex+".translateFrame") ($fileTex+".translateFrame");
  112.     connectAttr -f ($placeTex+".rotateFrame") ($fileTex+".rotateFrame");
  113.     connectAttr -f ($placeTex+".mirrorU") ($fileTex+".mirrorU");
  114.     connectAttr -f ($placeTex+".mirrorV") ($fileTex+".mirrorV");
  115.     connectAttr -f ($placeTex+".stagger") ($fileTex+".stagger");
  116.     connectAttr -f ($placeTex+".wrapU") ($fileTex+".wrapU");
  117.     connectAttr -f ($placeTex+".wrapV") ($fileTex+".wrapV");
  118.     connectAttr -f ($placeTex+".repeatUV") ($fileTex+".repeatUV");
  119.     connectAttr -f ($placeTex+".offset") ($fileTex+".offset");
  120.     connectAttr -f ($placeTex+".rotateUV") ($fileTex+".rotateUV");
  121.     connectAttr -f ($placeTex+".noiseUV") ($fileTex+".noiseUV");
  122.     connectAttr ($placeTex+".outUV") ($fileTex+".uv");
  123.     connectAttr ($placeTex+".outUvFilterSize") ($fileTex+".uvFilterSize");
  124.  
  125.     // Hook the file texture to the color
  126.     //
  127.     connectAttr -f ($fileTex+".outColor") ($lambert+".color");
  128.  
  129.     // Assign the source image.
  130.     // We use whatever is the default path.
  131.     // Enable frame extension and hardware texture cycling.
  132.     //
  133.     setAttr -type "string" ($fileTex+".fileTextureName") $imageName;
  134.     setAttr ($fileTex+".useFrameExtension") 1;
  135.     setAttr ($fileTex+".useHardwareTextureCycling") 1;
  136.     setAttr ($fileTex+".startCycleExtension") $startImage;
  137.     setAttr ($fileTex+".endCycleExtension")   $endImage;
  138.  
  139.     // Set expression on frameExtension to force file load and reset random generator
  140.     //
  141.     expression -s "if (frame <= 1) seed(1);\n\nint $start = startCycleExtension;\nint $end =   endCycleExtension;\nif (frame <= $end-$start+1)\n\tframeExtension = $start + frame - 1;\nelse\n\tframeExtension = $end;"  -o $fileTex -ae 1 -uc all ;
  142.  
  143.     // Assign the shader.
  144.     //
  145.     sets -e -forceElement $lambertSG $particleShape;
  146.  
  147.     // Add spriteNumPP attribute to the particle
  148.     // and write expression on it.
  149.     //
  150.     $test = `listAttr -st spriteNumPP $particleShape`;
  151.     if (size($test) == 0)
  152.     {
  153.         addAttr -ln spriteNumPP -dt doubleArray $particleShape;
  154.         addAttr -ln spriteNumPP0 -dt doubleArray $particleShape;
  155.     }
  156.  
  157.     // Upper and lower bounds for particle lifespan
  158.     //
  159.     setAttr ($particleShape+".lifespanMode") 2;
  160.     setAttr ($particleShape+".lifespan") (($minLifespan+$maxLifespan) / 2.0);
  161.     setAttr ($particleShape+".lifespanRandom") ($maxLifespan-$minLifespan);
  162.  
  163.  
  164.     // Enable/disable cycling
  165.     //
  166.     addAttr -ln "cycleEnabled" -at bool  $particleShape;
  167.     setAttr -e -keyable true ($particleShape+".cycleEnabled");
  168.     setAttr ($particleShape+".cycleEnabled") $cycleEnabled;
  169.  
  170.     // Random offset toggle
  171.     //
  172.     addAttr -ln "randomIndex" -at bool  $particleShape;
  173.     setAttr -e -keyable true ($particleShape+".randomIndex");
  174.     setAttr ($particleShape+".randomIndex") $randomIndex;
  175.  
  176.     
  177.     // Make the expressions
  178.     //
  179.     string $creationExprString;
  180.     $creationExprString = `dynExpression -q -c $particleShape`;
  181.     $creationExprString = "\nint $start = "+$fileTex+".startCycleExtension;\nint $end = "+$fileTex+".endCycleExtension;\nif (randomIndex)\n{\n\t// add random offset\n\tspriteNumPP = (int) $start + rand( 0, $end-$start+1);\n\n}" + $creationExprString;
  182.  
  183.     dynExpression -s $creationExprString -c $particleShape;
  184.  
  185.     string $runtimeExprString;
  186.     $runtimeExprString = `dynExpression -q -r $particleShape`;
  187.     $runtimeExprString = "\nint $start = "+$fileTex+".startCycleExtension;\nint $end = "+$fileTex+".endCycleExtension;\nint $index = $start;\nint $offset = 0;\nif ((!randomIndex) && (cycleEnabled))\n{\n\t// add offset for age\n\t$offset = (age/finalLifespanPP) * ($end-$start+1);\n\tspriteNumPP = $index + $offset;\n}" + $runtimeExprString;
  188.  
  189.     dynExpression -s $runtimeExprString -r $particleShape;
  190.  
  191.     return 0;
  192. }
  193.